package de.mfits.fireflyplayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import de.mfits.fireflyplayer.importer.LibraryImporter;
import de.mfits.fireflyplayer.importer.ServerConnection;
import de.mfits.fireflyplayer.importer.LibraryImporter.FinishedCallback;

public class MainActivity extends ListActivity {
  private static final String host = "data.forster.priv";
  private static final int port = 3689;

  private static final String LABEL_KEY = "label";

  private static final String ACTIVITY_KEY = "activity";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    List<Map<String, Object>> entries = new ArrayList<Map<String, Object>>();

    entries.add(createItem(R.string.browse_by_album, BrowseAlbumsActivity.class));
    entries.add(createItem(R.string.browse_by_artist, BrowseArtistsActivity.class));
    entries.add(createItem(R.string.browse_by_song, BrowseSongsActivity.class));
    entries.add(createItem(R.string.browse_by_genre, BrowseGenresActivity.class));

    SimpleAdapter adapter = new SimpleAdapter(this, entries, android.R.layout.simple_list_item_1,
        new String[] { LABEL_KEY }, new int[] { android.R.id.text1 });

    setListAdapter(adapter);
  }

  private Map<String, Object> createItem(int text, Class<? extends Activity> activityClass) {
    Map<String, Object> item = new HashMap<String, Object>();
    item.put(LABEL_KEY, getText(text));
    item.put(ACTIVITY_KEY, activityClass);
    return item;
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    Map<String, Object> item = (Map<String, Object>) getListView().getItemAtPosition(position);
    Class<?> activityClass = (Class<?>) item.get(ACTIVITY_KEY);
    final Intent intent = new Intent(this, activityClass);
    startActivity(intent);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    OnMenuItemClickListener listener = new OnMenuItemClickListener() {
      @Override
      public boolean onMenuItemClick(MenuItem item) {
        importLib();
        return true;
      }
    };
    menu.add(R.string.menu_item_import).setIcon(R.drawable.ic_menu_import)
        .setOnMenuItemClickListener(listener);
    menu.add(R.string.menu_item_refresh).setIcon(R.drawable.ic_menu_refresh)
        .setOnMenuItemClickListener(listener);

    return true;
  }

  private void importLib() {
    LibraryImporter importer;
    importer = new LibraryImporter(this, new FinishedCallback() {
      @Override
      public void onFinished(LibraryImporter importer) {
        Builder dlg =
            new AlertDialog.Builder(MainActivity.this).setNeutralButton(R.string.ok_button, null);
        Exception e = importer.getException();
        if (e != null) {
          dlg.setTitle(R.string.import_failure_title).setMessage(e.getMessage()).show();
        } else {
          dlg.setTitle(R.string.import_success_title).setMessage(R.string.import_success_message)
              .show();
        }
      }
    });
    importer.importLibrary(new ServerConnection(host, port));
  }
}